
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Xml;



namespace Swaf.Config
{
	/// <summary>
	/// The parent class for classes that want to participate in the application
	/// configuration file processing and this class provides set of helper methods
	/// for working with the XML document.</summary>
	/// <remarks>
	/// </remarks>
	/// <author>mdenman</author>
	public abstract class ConfigHandler : IConfigHandler
	{
		/// <summary>Derived classes must provide a definition for this method.
		/// This lets them get control at the beginning of a given XML form.
		/// </summary>
		public abstract void  executeBefore(IConfigElement configInfo);
		
		/// <summary>Derived classes can optionally override this method.
		/// This lets them get control at the <em>end</em> of a given XML form.
		/// </summary>
		public virtual void  executeAfter(IConfigElement configInfo)
		{
		}

		/*
		protected string getAttribute(IDictionary context, string path, XmlElement n, string attributeName, string defaultValue)
		{
			XmlAttribute p = n.GetAttributeNode(attributeName);
			return (p != null)?p.Value:defaultValue;
		}
		
		protected string getChildText(IDictionary context, string path, XmlElement n, string defaultValue)
		{
			if (n.ChildNodes.Count == 0)
				return defaultValue;
			XmlNode p = n.FirstChild;
			if (p.Name != "#text")
				return defaultValue;
			return p.Value;
		}
		
		protected string getChildText(IDictionary context, string path, XmlElement n)
		{
			return n.InnerText;
		}
		
		protected string getAttribute(IDictionary context, string path, XmlElement n, string attributeName)
		{
			XmlAttribute p = n.GetAttributeNode(attributeName);
			if (p == null)
				xmlError(context, "Missing attribute '" + attributeName + "=...'");
			return p.Value;
		}
		*/
		protected void xmlError(IDictionary context, string message)
		{
			throw new BadConfigException(String.Format("{0} : {1} : {2}",
				context["filename"], context["path"], message));
		}

		protected void xmlError(IDictionary context, string message, Exception e)
		{
			throw new BadConfigException(String.Format("{0} : {1} : {2}",
				context["filename"], context["path"], message), e);
		}
		
		/*
		/// <summary>
		/// Validates that the given <c>XmlElement</c> only contains attributes that are supposed to be there.</summary>
		/// <remarks>
		/// This method does not ensure that the legal attributes are actually being specified.
		/// It only ensures that any attributes that are being specified are known attributes.</remarks>
		/// <param name="context">A dictionary used as a calling context during the chain of processing.
		/// This is a convenient place to store info between the start and end tags of a xml node being
		/// processed.</param>
		/// <param name="path">The path the given node came from in the configuration process.</param>
		/// <param name="n">The xml element to look for attributes against.</param>
		/// <param name="names">A string of text that contains the legal attributes with a <c>|</c>
		/// starting the string and between each word.  For example:
		/// <code>|Id|Name|Month</code>
		/// For three legal attributes, id, name, and month.</param>
		protected void legalAttributes(IDictionary context, string path, XmlElement n, string names)
		{
			IList legal = Cmn.tokenizeOnFirstChar(names);
			XmlAttributeCollection attrs = n.Attributes;
			for(int p = 0; p < attrs.Count; ++p)
			{
				string a = attrs[p].Name;
				if (!legal.Contains(a))
					xmlError(context, "unexpected attribute " + a);
			}
			
		}
		*/
	}
}